#!/usr/bin/env python
# encoding: utf-8
"""
message.py

Created by 黄 冬 on 2007-12-14.
Copyright (c) 2007 __MyCompanyName__. All rights reserved.
"""

import logging.config
import socket

log = logging.getLogger('xbaytable.utils.message')

from xbaytable.conf import *
import binpack

def recv_head(connect):
    """收取消息头"""
    revmsg=connect.recv(6)
    log.debug('recv head is %s'%repr(revmsg))
    return binpack.unpackhead(revmsg)

def send_bind_table(connect,username,password):
    """向服务器发起bind请求，返回sequence_id"""
    from datetime import datetime
    now = datetime(1999,1,1).now()
    timestamp =  '%02i%02i%02i%02i%02i'%(now.month,now.day,now.hour,now.minute,now.second)
    #生成authentiator_code，长度为16位的MD5结果值
    import md5
    m=md5.new()
    m.update('%s%s%s'%(username,password,timestamp))
    authentiator_code=m.digest()
    sequence_id=get_sequence_id()
    #组包
    msg = binpack.genmsg(TABLE_BIND,sequence_id,username,authentiator_code,TABLE_VERSION,timestamp)
    #发送
    connect.send(msg)
    log.debug('bind_table_server send msg is %s'%repr(msg))
    return sequence_id

def process_bind_table(connect,command,sequence_id,msg):
    """处理bind_table的请求，返回0为bind成功，返回2为认证不对"""
    user_name,authentiator_code,version,timestamp=binpack.unpackbody(command,msg)
    user_name = user_name.rstrip('\x00')
    log.debug("bind username is %s"%user_name)
    import md5
    m=md5.new()
    try:
        m.update('%s%s%s'%(user_name,TABLE_USER[user_name],timestamp))
        if (authentiator_code==m.digest()):
            #上来的哥们是自家人
            msg = binpack.genmsg(TABLE_BIND_RESP,sequence_id,0,TABLE_VERSION)
            connect.send(msg)
            return 0
    except KeyError:
        pass
    #上来的哥们认证不对哟
    msg = binpack.genmsg(TABLE_BIN_RESP,sequence_id,2,TABLE_VERSION)
    connect.send(msg)
    return 2

def process_bind_table_resp(connect,command,sequence_id,msg):
    status,version=binpack.unpackbody(command,msg)
    if (status==0 and version==TABLE_VERSION):
        return 0
    return 1

def send_table_set(connect,name,value):
    """向服务发送set包
    参数说明：
    name 要set的name
    value 对应name的值
    返回值：
    发送出去的包的sequence_id"""
    sequence_id=get_sequence_id()
    msg = binpack.genmsg(TABLE_SET,sequence_id,len(name),name,value)
    connect.send(msg)
    #log.debug('send msg is %s'%repr(msg))
    return sequence_id

def send_table_get(connect,name):
    """向服务器发送get包
    参数说明：
    name 要get的name
    返回值：
    发送出去的包的sequence_id"""
    sequence_id=get_sequence_id()
    msg = binpack.genmsg(TABLE_GET,sequence_id,name)
    connect.send(msg)
    log.debug('table_get send msg is %s'%repr(msg))
    return sequence_id

def send_table_append(connect,name,value):
    """
    向服务器发送append数据包
    参数说明：
    name 要set的name
    value 对应name的追加值
    返回值：
    发送出去的包的sequence_id"""
    sequence_id=get_sequence_id()
    msg = binpack.genmsg(TABLE_APPEND,sequence_id,len(name),name,value)
    connect.send(msg)
    #log.debug('send msg is %s'%repr(msg))
    return sequence_id

def send_table_del(connect,name):
    """
    向服务器发送del包
    参数说明：
    name 要del的name
    返回值：
    发送出去的包的sequence_id
    """
    sequence_id=get_sequence_id()
    msg = binpack.genmsg(TABLE_DEL,sequence_id,name)
    connect.send(msg)
    log.debug('send msg is %s'%repr(msg))
    return sequence_id
